package _02_event_stream._2_0_0_listener

import akka.actor._

/**
  * @author galudisu
  */

abstract class AllKindsOfMusic {
  def artist: String
}
case class Jazz(artist: String) extends AllKindsOfMusic
case class Electronic(artist: String) extends AllKindsOfMusic

class Listener extends Actor {
  override def receive: Receive = {
    case m: Jazz ⇒ println(s"${self.path.name} is listening to: ${m.artist}")
    case m: Electronic ⇒ println(s"${self.path.name} is listening to: ${m.artist}")
  }
}


object Listener extends App {
  implicit val system       = ActorSystem("QuickStart")
  implicit val ec           = system.dispatcher

  val jazzListener = system.actorOf(Props[Listener])
  val musicListener = system.actorOf(Props[Listener])
  system.eventStream.subscribe(jazzListener, classOf[Jazz])
  system.eventStream.subscribe(musicListener, classOf[AllKindsOfMusic])

  // only musicListener gets this message, since it listens to *all* kinds of music:
  system.eventStream.publish(Electronic("Parov Stelar"))

  // jazzListener and musicListener will be notified about Jazz:
  system.eventStream.publish(Jazz("Sonny Rollins"))

}
